package com.wx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.domain.Drug;
import com.wx.service.DrugService;
import com.wx.service.impl.DrugServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/findDrugByName")
public class FindDrugByName extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        String drugName = req.getParameter("drugName");

        DrugService drugService=new DrugServiceImpl();
        ObjectMapper mapper=new ObjectMapper();
        Map<String, Object> map=new HashMap<>();

        List<Drug> list = drugService.getDrugByName(drugName);

        map.put("list",list);
        map.put("pageTotal",8);
        mapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
